//下拉菜单模块
define(['jquery', 'loadcss'], function($, loadcss) {
    loadcss('../src/propments/css/dropmenu.css', document.body);
    var dropdowns = $(".hc-drop"); //下拉菜单

    //模块默认的定义的属性
    var option = {
        type: 'bottom',
        donghua: true,
    };

    function set(obj) {
        //能set函数替换所有下拉菜单的默认属性
    }

    //初始化 下拉菜单 
    function init() {
        dropdowns.each(function(index, ele) {
            var btn = $(this).find(".btn");
            var ul = $(this).find("ul");
            var name = $(this).attr('name');
            var list = $(this).attr("list");
            var type = $(this).attr('type');


            //加载菜单按钮
            if (btn.length == 0) {
                btn = $(this).append('<button class="btn">' + name + '</button>').find(".btn");
            }
            //加载子菜单
            if (ul.length == 0) {
                if (list == undefined) return;
                var html_li = "<ul>";
                var arr = list.split(',');
                for (var i in arr) {
                    html_li += "<li>" + arr[i] + "</li>"
                }
                html_li += "</ul>"
                ul = $(this).append(html_li).find("ul");
            }

            //设置下拉菜单的位置
            if (type == undefined) type = 'bottom';
            var btn_w = btn.outerWidth();
            var btn_h = btn.outerHeight();
            var ul_w = ul.width();
            var ul_h = ul.height();
            console.log(btn_h);
            switch (type) {
                case 'top':
                    ul.css('top', '-' + (btn_h + ul_h) + 'px');
                    break;
                case 'right':
                    ul.css('left', btn_h + 'px');
                    break;
                case 'bottom':
                    ul.css('top', btn_h + 'px');
                    break;
                case 'left':
                    ul.css('left', '-' + (btn_w + ul_w) + 'px');
                    break;
            }

        });

    }

    //绑定点击事件
    function bindClick() {
        dropdowns.on('click', function() {
            $(this).find("ul").toggle();
        });
    }

    function run() {
        init();
        bindClick();
    }

    return {
        run: run,
        set: set,
    }
});